package org.bluedream.core.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.sys.entity.Store;
import org.bluedream.core.module.sys.service.StoreService;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StoreController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-11-23 14:16
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/store")
public class StoreController extends BaseController {

    @Resource
    private StoreService storeService;

    @ModelAttribute
    public Store get(String id){
        return storeService.getById(id);
    }

    @RequiresPermissions("store:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Store entity, Model model){
        model.addAttribute("isSystem" , UserUtil.isSystem());
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("store" , entity);
        return "sys/store/storeList";
    }

    @RequiresPermissions("store:view")
    @RequestMapping("/form")
    public String form(Store entity, Model model){
        model.addAttribute("store" , entity);
        return "sys/store/storeForm";
    }

    @RequiresPermissions("store:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Store> findPage(@RsaSecurityParameter Store entity){
        return storeService.findPage(entity);
    }

    @RequiresPermissions("store:view")
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return storeService.treeData(excludeCode , isShowCode);
    }

    @RequiresPermissions("store:edit")
    @RequestMapping("/save")
    @ResponseBody
    public RestResult save(Store entity){
        return storeService.saveEntity(entity);
    }
}
